package com.llgz.hotelmanageapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llgz.hotelmanageapi.pojo.DO.CheckinInfo;
import com.llgz.hotelmanageapi.mapper.CheckinInfoMapper;
import com.llgz.hotelmanageapi.pojo.DTO.CheckinPageDTO;
import com.llgz.hotelmanageapi.pojo.VO.checkinPageVo;
import com.llgz.hotelmanageapi.pojo.DO.OrderInfo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.llgz.hotelmanageapi.mapper.*;
import com.llgz.hotelmanageapi.pojo.DAO.CheckinInfoDAO;
import com.llgz.hotelmanageapi.pojo.DO.*;
import com.llgz.hotelmanageapi.service.CheckinInfoServer;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 入住信息 服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2021-07-19
 */
@Service
public class CheckinInfoServiceImpl extends ServiceImpl<CheckinInfoMapper, CheckinInfo> implements CheckinInfoServer {

    @Autowired
    private CheckinInfoMapper checkinInfoMapper;

    @Override
    public IPage<checkinPageVo> queryCheckinInfo(CheckinPageDTO checkinPageDTO) {
        int pageSize = checkinPageDTO.getPageSize();
        int pageNum = (checkinPageDTO.getPageNum() - 1) * pageSize;
        List<checkinPageVo> pageList = checkinInfoMapper.queryCheckinInfo(pageNum, pageSize, checkinPageDTO.getRoomId(),
                checkinPageDTO.getRoomType(), checkinPageDTO.getOrderId(), checkinPageDTO.getName());
        int total = checkinInfoMapper.queryCheckinInfoTotal(pageNum, pageSize, checkinPageDTO.getRoomId(),
                checkinPageDTO.getRoomType(), checkinPageDTO.getOrderId(), checkinPageDTO.getName());
        Page<checkinPageVo> checkinInfoPage = new Page<>(pageNum, pageSize);
        checkinInfoPage.setRecords(pageList);
        checkinInfoPage.setTotal(total);
        return checkinInfoPage;
    }

    @Autowired
    private OrderInfoServiceImpl orderInfoService;

    @Autowired
    private RoomInfoServiceImpl roomInfoService;
    @Autowired

    private RoomReserveInfoServiceImpl roomReserveInfoService;

    @Autowired
    private OrderPersonServiceImpl orderPersonService;

    @Override
    public List<List<String>> getCheckInExcelHead() {
        List<List<String>> headList = new LinkedList<>();
        List<String> head0 = new LinkedList<>();
        head0.add("入住单号");
        List<String> head1 = new LinkedList<>();
        head1.add("客房编号");
        List<String> head2 = new LinkedList<>();
        head2.add("客房类型");
        List<String> head3 = new LinkedList<>();
        head3.add("入住人");
        List<String> head4 = new LinkedList<>();
        head4.add("会员编号");
        List<String> head5 = new LinkedList<>();
        head5.add("抵店时间");
        List<String> head6 = new LinkedList<>();
        head6.add("离店时间");
        List<String> head7 = new LinkedList<>();
        head7.add("联系电话");

        headList.add(head0);
        headList.add(head1);
        headList.add(head2);
        headList.add(head3);
        headList.add(head4);
        headList.add(head5);
        headList.add(head6);
        headList.add(head7);
        return headList;
    }

    @Override
    public List<List<Object>> getCheckInDataByDateAndRoomId(String startDayTime, String endDayTime, Integer roomId) {
        QueryWrapper<CheckinInfo> checkinInfoQueryWrapper = new QueryWrapper<>();
        checkinInfoQueryWrapper.apply("UNIX_TIMESTAMP(created_time) >= UNIX_TIMESTAMP('" + startDayTime + "')");
        checkinInfoQueryWrapper.apply("UNIX_TIMESTAMP(created_time) < UNIX_TIMESTAMP('" + endDayTime + "')");

        List<CheckinInfo> checkinInfoList = checkinInfoMapper.selectList(checkinInfoQueryWrapper);

        if (checkinInfoList.size() == 0) {
            return new LinkedList<>();
        }

        List<String> orderIdList = checkinInfoList.stream().map(CheckinInfo::getOrderId).collect(Collectors.toList());


        List<OrderInfo> orderInfoList = orderInfoService.listByIds(orderIdList);
        HashMap<String, OrderInfo> orderMap = new HashMap<>();
        orderInfoList.forEach(orderInfo -> orderMap.put(orderInfo.getId(), orderInfo));


        List<List<Object>> allRowDataList = new LinkedList<>();

        for (CheckinInfo checkinInfo : checkinInfoList) {
            List<Object> rowData = new LinkedList<>();
            OrderInfo orderInfo = orderMap.get(checkinInfo.getOrderId());
            // roomId筛选
            if (roomId != null && !orderInfo.getRoomId().equals(roomId)) {
                continue;
            }

            OrderPerson orderPerson = orderPersonService.getOne(new QueryWrapper<OrderPerson>().eq("order_id", checkinInfo.getOrderId()),false);
            rowData.add(checkinInfo.getId()); // 入住单号
            rowData.add(orderInfo.getRoomId()); // 客房编号
            rowData.add(orderInfo.getRoomType()); // 客房类型
            rowData.add(orderPerson.getName()); // 入住人
            rowData.add(orderInfo.getMemberId()); // 会员编号
            rowData.add(orderInfo.getArrivedTime()); // 抵店时间
            rowData.add(orderInfo.getDepartureTime()); // 离店时间
            rowData.add(orderInfo.getContactTel()); // 联系电话

            allRowDataList.add(rowData);
        }
        return allRowDataList;
    }

    @Override
    public Boolean checkIn(CheckinInfoDAO checkinInfoDAO) {
        String orderId = checkinInfoDAO.getOrderId();
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(checkinInfoDAO, orderInfo);
        orderInfo.setId(orderId);
        orderInfo.setOrderStatus(1);
//       更新订单信息
        orderInfoService.updateById(orderInfo);

        CheckinInfo checkinInfo = new CheckinInfo();
        BeanUtils.copyProperties(checkinInfoDAO, checkinInfo);
//        添加入住信息
        checkinInfoMapper.insert(checkinInfo);

//      修改房间状态
        roomInfoService.updateRoomStatusByRoomId(checkinInfoDAO.getRoomId(), 2);
//        修改预定状态
        roomReserveInfoService.modifyReserveStatusByOrderId(orderId, 1);
//        增加入住人员
        orderPersonService.saveBatch(checkinInfoDAO.getPeopleList());
        return true;

//        UpdateWrapper<RoomReserveInfo> reserveInfoUpdateWrapper = new UpdateWrapper<>();
//        reserveInfoUpdateWrapper.eq("order_id",orderId);
//        reserveInfoUpdateWrapper.set("reserve_status",1);//        roomReserveInfoMapper.update(null,reserveInfoUpdateWrapper);

    }
}
